package sample.business.frame.modules;

import com.google.common.collect.Maps;
import sample.business.frame.DirVo;
import sample.business.util.FreemarkerUtil;
import sample.business.frame.Constants;
import sample.data.PathInfo;

import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * Created by lilongfei on 2016/12/18.
 */
public abstract class ModuleGeneratorImpl implements IModuleGenerator {

    private DirVo dirVo;

    private PathInfo pathInfo;

    @Override
    public void mkBasicDirs() {
        createDir(dirVo.getJavaDir());
        createDir(dirVo.getResourceDir());
        createDir(dirVo.getContextDir());
        createDir(dirVo.getTestJavaDir());
        createDir(dirVo.getTestResourcesDir());
    }

    @Override
    public void mkWebDirs() {
        File file = new File(dirVo.getWebappDir());
        file.mkdirs();
        file = new File(dirVo.getWebInfo());
        file.mkdir();
    }

    protected void createDir(String path){
        File file = new File(path);
        file.mkdirs();
    }

    public abstract void generateProject() throws Exception;

    @Override
    public void createPom(String path) throws Exception {
        Map<String,Object> map = Maps.newHashMap();
        map.put("path",pathInfo);
        FreemarkerUtil.createXml("global.ftl",map,path + File.separator + Constants.POM);

    }

    public ModuleGeneratorImpl setPathInfo(PathInfo pathInfo) {
        this.dirVo = new DirVo(pathInfo);
        this.pathInfo = pathInfo;
        return this;
    }

    public DirVo getDirVo() {
        return dirVo;
    }

}
